package com.szeldon.pj.jps.exp;

import java.util.logging.Logger;

import com.szeldon.pj.jps.datastore.IntegerObject;
import com.szeldon.pj.jps.datastore.RealObject;
import com.szeldon.pj.jps.datastore.SimpleObject;

public class IntegerTerminalExpression extends TerminalExpression {

	private static final Logger logger = Logger.getLogger(IntegerTerminalExpression.class.getName());

	private int value;

	public IntegerTerminalExpression(int value) {
		this.value = value;
	}

	@Override
	public String getValueAsString() {
		return "" + value;
	}

	public int getValue() {
		return value;
	}

	@Override
	public int compareTo(SimpleObject object) throws IllegalStateException {
		double objectValue = 0;
		if (object instanceof IntegerObject) {
			objectValue = ((IntegerObject) object).getValue();
		} else if (object instanceof RealObject) {
			objectValue = ((RealObject) object).getValue();
		} else {
			throw new IllegalStateException("IntegerTerminalExpression can't be compared with object of"
					+ object.getClass().getName() + " type.");
		}

		logger.info("comparing " + value + " with " + objectValue);

		if (value < objectValue) {
			return -1;
		} else if (value == objectValue) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public int compareTo(TerminalExpression expression) {
		double objectValue = 0;
		if (expression instanceof IntegerTerminalExpression) {
			objectValue = ((IntegerTerminalExpression) expression).getValue();
		} else if (expression instanceof RealTerminalExpression) {
			objectValue = ((RealTerminalExpression) expression).getValue();
		} else {
			throw new IllegalStateException("IntegerTerminalExpression can't be compared with expression of"
					+ expression.getClass().getName() + " type.");
		}

		logger.info("comparing " + value + " with " + objectValue);

		if (value < objectValue) {
			return -1;
		} else if (value == objectValue) {
			return 0;
		} else {
			return 1;
		}
	}

}
